package com.example.controller;

import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.result.EmployeeResult;
import com.example.result.JsonResult;
import com.example.service.EmployeeService;
import com.example.untils.MD5encrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.domain.Employee;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController{   //员工表
    @Autowired
    private EmployeeService employeeService;
    //员工登录
    @PostMapping("/login")
    public JsonResult login(HttpSession session,@RequestBody Employee employee){
        //查询
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee result = employeeService.getOne(queryWrapper);
        //判断
        if(result==null){
            throw new RuntimeException("账户不存在");
        }
        //密码加密
        String md5Password = MD5encrypt.getMD5Password(employee.getPassword(), result.getSalt());
        if(!result.getPassword().equals(md5Password)){
            throw new RuntimeException("密码不正确");
        }
        if (result.getStatus()==0){
            throw new RuntimeException("该账号已被锁定");
        }
        //存储session中
        EmployeeResult employeeResult = new EmployeeResult(result);
        session.setAttribute("employee",employeeResult);
        return new JsonResult(1,"登录成功",employeeResult);
    }
    //员工退出
    @PostMapping("/logout")
    public JsonResult logout(HttpSession session){
        session.removeAttribute("employee");
        return new JsonResult(1,"退出成功");
    }
    //新增员工
    @PostMapping
    public JsonResult save(@RequestBody Employee employee,HttpSession session){
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee result = employeeService.getOne(queryWrapper);
        if(result!=null){
            throw new RuntimeException("用户名已存在!");
        }
        //生成随机盐值
        String salt= UUID.randomUUID().toString().toUpperCase();
        employee.setSalt(salt);
        //密码加密
        employee.setPassword(MD5encrypt.getMD5Password("123456",salt));

        boolean flag = employeeService.save(employee);
        if(!flag){
            throw new RuntimeException("新增失败");
        }
        return new JsonResult(1,"新增成功");
    }
    //员工列表分页查询
    @GetMapping("/page")
    public JsonResult page(Integer page,Integer pageSize,String name){
        IPage<Employee> iPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(Strings.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(iPage,queryWrapper);

        return new JsonResult(1,"",iPage);
    }
    //禁用或启用员工按钮
    @PutMapping
    public JsonResult update(@RequestBody Employee employee,HttpSession session){
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId,employee.getId());
        Employee result = employeeService.getOne(wrapper);
        if(result==null){
            throw new RuntimeException("员工信息不存在");
        }
        employee.setUpdateUser(getSessionId(session));
        employee.setUpdateTime(LocalDateTime.now());
        boolean flag = employeeService.updateById(employee);
        if(!flag){
            throw new RuntimeException("员工信息操作失败");
        }
        return new JsonResult(1,"操作成功");
    }
    //修改员工信息
    @PutMapping("/update")
    public JsonResult update(HttpSession session,@RequestBody Employee employee){
        Long sessionId = getSessionId(session);
        boolean flag = employeeService.save(employee);
        if(!flag){
            throw new RuntimeException("修改失败,请刷新!");
        }
        return new JsonResult(1,"信息修改成功");
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public JsonResult getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if(employee==null){
            throw new RuntimeException("员工信息不存在，请刷新!");
        }
        return new JsonResult(1,"",employee);
    }
}
